package listbyorder.access101_200.test169;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/14 13:49
 */
public class Solution3 {

    // 解法三： 摩尔投票法
    public int majorityElement(int[] nums) {
        int count = 1;
        int group = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (count == 0) {
                count = 1;
                group = nums[i];
                continue;
            }
            if (nums[i] == group) {
                // 遇到了同组则人数加1
                count++;
            } else {
                count--;
            }
        }
        return group;
    }
}
